package com.canbot.u05.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.StyleableRes;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.canbot.u05.R;

/**
 * 刻度尺
 * Created by Administrator on 2016/9/14.
 */
public class ScaleView extends View {

        private Paint mLinePaint;

        private Paint mTextPaint;

        private Paint mRulerPaint;

        private float progrees = 5;

        private int max = 180;

        private int min = 0;

        private int index;

        public void setDuration(int duration) {
                this.duration = duration;
                invalidate();
        }

        private int duration = 0;

        private boolean isCanMove;

        private int playStatus = 0;

        private OnScrollListener mScrollListener;

        private OnPlayListener onPlayListener;

        public static final int[] ATTR = {
                R.attr.lf_scale_view_min,
                R.attr.lf_scale_view_max,
                R.attr.lf_scale_view_duration
        };

        public static final @StyleableRes
        int LF_SCALE_MIN = 0;

        public static final @StyleableRes
        int LF_SCALE_MAX = 1;

        public static final @StyleableRes
        int LF_SCALE_DURATION = 2;

        public ScaleView(Context context) {
                super(context);
                init(null);
        }

        public ScaleView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init(attrs);
        }

        public ScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                init(attrs);
        }

        public static final int START_PLAY = 1;

        public int currentTime = 0;

        @SuppressLint("HandlerLeak")
        private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        switch (msg.what) {
                                case START_PLAY:
                                        if (currentTime <= duration) {
                                                invalidate();
                                        }
                                        break;
                                default:
                                        break;
                        }
                }
        };

        private void init(AttributeSet attrs) {
                // 获取自定义属性
                TypedArray ta = getContext().obtainStyledAttributes(attrs, ATTR);
                min = ta.getInteger(LF_SCALE_MIN, 0);
                max = ta.getInteger(LF_SCALE_MAX, 180);
                duration = ta.getInteger(LF_SCALE_DURATION, 0);
                mLinePaint = new Paint();
                mLinePaint.setColor(getResources().getColor(R.color.scale_line1, null));
                mLinePaint.setAntiAlias(true);//抗锯齿
                mLinePaint.setStyle(Paint.Style.STROKE);
                mLinePaint.setStrokeWidth(2);
                mTextPaint = new Paint();
                mTextPaint.setColor(getResources().getColor(R.color.scale_text, null));
                mTextPaint.setAntiAlias(true);
                mTextPaint.setStyle(Paint.Style.FILL);
                mTextPaint.setStrokeWidth(2);
                mTextPaint.setTextSize(18);
                //
                mRulerPaint = new Paint();
                mRulerPaint.setAntiAlias(true);
                mRulerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mRulerPaint.setColor(Color.RED);
                mRulerPaint.setStrokeWidth(2);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                setMeasuredDimension(setMeasureWidth(widthMeasureSpec), setMeasureHeight(heightMeasureSpec));
        }

        private int setMeasureHeight(int spec) {
                int mode = MeasureSpec.getMode(spec);
                int size = MeasureSpec.getSize(spec);
                int result = Integer.MAX_VALUE;
                switch (mode) {
                        case MeasureSpec.AT_MOST:
                                size = Math.min(result, size);
                                break;
                        case MeasureSpec.EXACTLY:
                                break;
                        default:
                                size = result;
                                break;
                }
                return size;
        }

        private int setMeasureWidth(int spec) {
                int mode = MeasureSpec.getMode(spec);
                int size = MeasureSpec.getSize(spec);
                int result = Integer.MAX_VALUE;
                switch (mode) {
                        case MeasureSpec.AT_MOST:
                                size = Math.min(result, size);
                                break;
                        case MeasureSpec.EXACTLY:
                                break;
                        default:
                                size = result;
                                break;
                }
                return size;
        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                canvas.save();
                for (int i = min; i <= max; i++) {
                        if (i <= currentTime && currentTime < duration && playStatus == 1) {
                                mLinePaint.setColor(getResources().getColor(R.color.scale_line2, null));
                        }
                        else {
                                mLinePaint.setColor(getResources().getColor(R.color.scale_line1, null));
                        }
                        if (i % 10 == 0) {
                                canvas.drawLine(10, 0, 10, 32, mLinePaint);
                                String text = i / 10 * 10 + "";
                                Rect rect = new Rect();
                                float txtWidth = mTextPaint.measureText(text);
                                mTextPaint.getTextBounds(text, 0, text.length(), rect);
                                mTextPaint.setTextSize(18);
                                if (i / 10 * 10 != 0 && i / 10 * 10 % 180 == 0) {
                                        //每一页刻度最后一个值显示单位
                                        canvas.drawText(text + "s", 10 - txtWidth / 2, 32 + rect.height() + 10, mTextPaint);
                                }
                                else {
                                        canvas.drawText(text, 10 - txtWidth / 2, 32 + rect.height() + 10, mTextPaint);
                                }
                        }
                        else {
                                canvas.drawLine(10, 10, 10, 20, mLinePaint);
                        }
                        canvas.translate(8, 0);
                }
                canvas.restore();

                if (playStatus == 1) {
                        currentTime++;
                        int scale = (int) (((currentTime * 8 + 5) - 5) / 8);
                        if (mScrollListener != null) {
                                mScrollListener.onScaleScroll(scale);

                        }
                        if (currentTime<max){
                                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pointer), (currentTime * 8 + 5) - 2, 0, mRulerPaint);
                        }else {
                                Log.d("ScaleView", "====刻度尺改变==="+currentTime+", "+max);
                                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pointer), (180 * 8 + 5) - 2, 0, mRulerPaint);
                                min++;
                                max++;
                        }
                        if (onPlayListener != null) {
                                onPlayListener.onPlayStatus(1);
                        }
                        if (currentTime < duration) {
                                handler.sendEmptyMessageDelayed(START_PLAY, 1000);
                        }
                        else {
                                if (onPlayListener != null) {
                                        onPlayListener.onPlayStatus(3);
                                }
                                currentTime = 0;

                        }
                        return;
                }

                int scale = (int) ((progrees - 5) / 8);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pointer), progrees - 2, 0, mRulerPaint);
                Log.d("ScaleView", "progrees:" + progrees + ", " + (scale+index*180));
                if (mScrollListener != null) {
                        mScrollListener.onScaleScroll(min+scale);

                }


        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                                isCanMove = true;
                                break;
                        case MotionEvent.ACTION_MOVE:
                                if (!isCanMove) {
                                        return false;
                                }

                                if (playStatus == 1){
                                        Log.d("ScaleView","预览中指针不能滑动");
                                        return false;
                                }
                                progrees = event.getX();
                                int scale = (int) ((progrees - 5) / 8);
                                index = max/181;
                                Log.d("ScaleView", "scale: "+scale + ", min: " + min + ", max: " + max+", index: "+ index +", "+(scale + index *180));
                                if (scale<-2){
                                        return false;

                                }
                                if (scale<0){
                                        Log.d("ScaleView", "====刻度尺改变===");
                                        if (min>0){
                                                min--;
                                                max--;
                                                invalidate();
                                        }
                                }else {
                                        if (scale + index *180 < max && scale + index *180 > min) {
                                                Log.d("ScaleView", "刻度尺不改变");
                                                invalidate();
                                        }
                                        else {
                                                if (scale>182){
                                                        return false;
                                                }
                                                if (scale> 180){
                                                        if (max <= duration+2){
                                                                Log.d("ScaleView", "====刻度尺改变===");
                                                                min++;
                                                                max++;
                                                                invalidate();
                                                        }
                                                }else {
                                                        Log.d("ScaleView", "刻度尺不改变");
                                                        invalidate();
                                                }
                                        }
                                }
                                break;
                }
                return true;
        }

        public void play() {
                playStatus = 1;
                handler.sendEmptyMessageDelayed(START_PLAY, 1000);
        }

        public void stop() {
                currentTime = 0;
                playStatus = 3;
        }

        public void pause() {
                playStatus = 2;
                //暂时
                currentTime = 0;
        }

        /**
         * 设置回调监听
         *
         * @param listener
         */
        public void setOnScrollListener(OnScrollListener listener) {
                this.mScrollListener = listener;
        }

        public void setOnPlayListener(OnPlayListener onPlayListener) {
                this.onPlayListener = onPlayListener;
        }

        public interface OnScrollListener {

                void onScaleScroll(int scale);
        }

        public interface OnPlayListener {

                void onPlayStatus(int status);
        }
}